package hai.com.android_test.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * 基础 Fragment
 */
public abstract class BaseFragment extends Fragment {

	protected Context context;
	private View rootView;
	// 是否已加载数据
	private Boolean hasInitData = false;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (rootView == null) {
			rootView = initView(inflater, container, savedInstanceState);
			ButterKnife.bind(this, rootView);
			afterBind();
		}
		return rootView;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (!hasInitData) {
			initData();
			hasInitData = true;
		}
	}

	public void onDestroyView() {
		super.onDestroyView();
		((ViewGroup) rootView.getParent()).removeView(rootView);
	}

	/**
	 * 子类实现初始化View操作
	 * 
	 * @param inflater
	 * @return
	 */
	protected abstract View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

	/** 绑定控件后操作 **/
	public void afterBind() {

	}

	/**
	 * 子类实现初始化数据操作
	 */
	public abstract void initData();

	public void onResume() {
		super.onResume();
	}

	public void onPause() {
		super.onPause();
	}
}
